package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class EsDocumentDemo {

    @Autowired
    IHotelService iHotelService;

    @Test
    public void addDocument() throws IOException {
        //从数据库根据id查询一条文档数据==>Hotel
        Hotel hotel = iHotelService.getById(36934);
        //将Hotel对象   转为====>horelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //将Hotel对象  保存到ES索引库
        //创建添加文档的请求对象
        IndexRequest request = new IndexRequest("hotel");
        //设置文档id
        request.id(hotelDoc.getId() + "");
        //设置文档内容
        String jsonStr = JSON.toJSONString(hotelDoc);
        request.source(jsonStr, XContentType.JSON);
        //调用客户端执行请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //得到相应结果
        System.out.println(response.getResult());
    }

    @Test
    public void getDocumentById() throws IOException {
        //创建查询文档的请求对象
        GetRequest request = new GetRequest("hotel", "36943");
        //执行查询文档请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //解析响应得到的文档结果
        String hotelJsonStr = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(hotelJsonStr, HotelDoc.class);
        System.out.println(hotelDoc);

    }

    @Test
    public void updateById() throws IOException {
        //创建修改文档的请求对象
        UpdateRequest request = new UpdateRequest("hotel", "36934");
        request.doc("price", "398");
        //执行修改文档请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        //解析响应得到的修改文档结果
        System.out.println(response.getResult());

    }

    @Test
    public void deleteById() throws IOException {
        //创建删除文档的请求对象
        DeleteRequest request = new DeleteRequest("hotel","36934");
        //执行删除文档请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //解析响应得到的删除文档结果
        System.out.println(response.getResult());

    }

    @Test
    public void bulkAddDocument() throws IOException {
//        //查询所有数据库中酒店数量,查询全部
//        List<Hotel> list = iHotelService.list();
//
//        //批量的添加到ES索引中
//        BulkRequest bulkRequest = new BulkRequest("hotel");
//        for (Hotel hotel : list) {
//            //for循环遍历   要批量插入的酒店列表  每一个酒店数据    都封装成一个IndexRequest对象
//            IndexRequest request = new IndexRequest();
//            request.id(hotel.getId() + "");// 设置文档
//            HotelDoc hotelDoc = new HotelDoc(hotel);
//            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
//            //  将 添加文档的请求  加如到批处理请求中
//            bulkRequest.add(request);
//        }
//        // 执行批处理请求
//        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//        //打印结果   ok代表出成功
//        System.out.println(bulk.status());


        // 批量查询酒店数据
        List<Hotel> hotels = iHotelService.list();

        // 1.创建Request
        BulkRequest request = new BulkRequest();
        // 2.准备参数，添加多个新增的Request
        for (Hotel hotel : hotels) {
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
        }
        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);

    }


    RestHighLevelClient client;

    @BeforeEach
    public void init() {
        //1. 准备号客户端对象
        client = new RestHighLevelClient
                (RestClient.builder(HttpHost.create("http://192.168.200.130:9200")));

    }

    @AfterEach
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
